package com.zzl.bonds.ticket.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.bonds.ticket.dto.credit.BwBankAddDto;
import com.zzl.bonds.ticket.dto.credit.SupplierAvailableCreditDto;
import com.zzl.bonds.ticket.dto.finance.SupplierCreditAddDto;
import com.zzl.bonds.ticket.dto.finance.SupplierCreditUpdateDto;
import com.zzl.bonds.ticket.entity.SupplierCreditEntity;
import com.zzl.bonds.ticket.service.SupplierCreditService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 供应商授信额度表,即集团给供应商的额度
 *
 * @author sunpp
 * @date 2022-02-16 14:04:04
 */
@ApidocController("2供应商白名单的额度配置（供应商授信额度表）")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("bondsticket/suppliercredit")
@Slf4j
public class SupplierCreditController extends WebBaseCtrl {

    @SuppressWarnings("all")
    @Autowired
    private SupplierCreditService supplierCreditService;

    @GetMapping("/getSelectCredit")
    @ApidocOperation(description = "1凭证融资-融单额度下拉框", operationId = "ticket/suppliercredit/getSelectCredit")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = SupplierCreditEntity.class)
    })
    public Result getSelectCredit() {
        return Result.ok().put("list", supplierCreditService.getFinanceCreditList(this.getUser()));
    }

    /**
     * 可用融资额度恢复到 最 初始的 额度
     */
    @GetMapping("/resetSupplierCredit/{enterpriseId}")
    @ApidocOperation(description = "供应商白名单-重置额度按钮", operationId = "ticket/resetSupplierCredit/enterpriseId")
    @ApidocReturnR
    public Result resetSupplierCredit(@ApidocParam(name = "enterpriseId", description = "企业id", required = true)
                                      @PathVariable("enterpriseId") String enterpriseId) {
        try {
            supplierCreditService.resetSupplierCredit(enterpriseId, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    @GetMapping("/resetFinancingCredit/{id}")
    @ApidocOperation(description = "凭证融资限额列表-重置按钮", operationId = "ticket/resetFinancingCredit/enterpriseId")
    @ApidocReturnR
    public Result resetFinancingCredit(@ApidocParam(name = "id", description = "融资额度id", required = true)
                                       @PathVariable("id") String id) {
        try {
            supplierCreditService.resetFinancingCredit(id, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }


    @PostMapping("/saveSupplierCredit")
    @ApidocOperation(description = "单个新增或者 修改金融机构给 供应商的 融资额度", operationId = "ticket/updateAndSaveSupplierCredit1/saveSupplierCredit")
    @ApidocReturnR
    public Result saveSupplierCredit(@ApidocParam(name = "添加的对象", required = true) @RequestBody @Validated BwBankAddDto bwBankAddDto) {
        try {
            supplierCreditService.updateAndSaveSupplierCredit(Arrays.asList(bwBankAddDto), this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 可给当前供应商开票额度
     */
    @GetMapping("/availableCredit")
    @ApidocOperation(description = "可给当前供应商开票额度", operationId = "ticket/institutycredit/availableCredit")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", valueClass = SupplierAvailableCreditDto.class)
    })
    public Result availableCredit(@ApidocParam(name = "enterpriseId", description = "供应商id", required = true)
                                  @RequestParam("enterpriseId") String enterPriseId,
                                  @ApidocParam(name = "institutyId", description = "金融机构id", required = true)
                                  @RequestParam("institutyId") Integer institutyId,
                                  @ApidocParam(name = "id", description = "核心企业授信id", required = true)
                                  @RequestParam("id") String id) {
        try {
            return Result.ok().put("data", supplierCreditService.availableCredit(enterPriseId, institutyId, id,
                    this.getUser()));
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e.getMsg());
        }
    }

    /**
     * 新增或者 修改金融机构给 供应商的 开票额度
     */
    @PostMapping("/updateAndSaveSupplierCredit")
//    @ApidocOperation(description = "批量新增或者 修改金融机构给 供应商的融资额度", operationId = "ticket/updateAndSaveSupplierCredit1")
    @ApidocReturnR
    public Result updateAndSaveSupplierCredit(@ApidocParam(name = "添加的对象", required = true) @RequestBody @Validated List<BwBankAddDto> bankShowDtos) {
        try {
            supplierCreditService.updateAndSaveSupplierCredit(bankShowDtos, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }


    /**
     * 列表
     */
    @GetMapping("/list")
    @ApidocOperation(description = "分页查询供应商授信额度表,即集团给供应商的额度数据", operationId = "ticket/suppliercredit/list")
    @ApidocReturnPage(genericTypeClass = SupplierCreditEntity.class)
    public Result list(@ApidocParamsPage(name = "params") @RequestParam Map<String, Object> params) {
        PageUtils page = supplierCreditService.queryPage(params);
        return Result.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "通过id获取供应商授信额度表,即集团给供应商的额度数据", operationId = "ticket/suppliercredit/info")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "supplierCredit", valueClass = SupplierCreditEntity.class)
    })
    public Result info(@ApidocParam(name = "供应商授信额度表,即集团给供应商的额度id", required = true) @PathVariable("id") Integer id) {
        SupplierCreditEntity supplierCredit = supplierCreditService.getById(id);
        return Result.ok().put("supplierCredit", supplierCredit);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApidocOperation(description = "保存供应商授信额度表,即集团给供应商的额度", operationId = "ticket/suppliercredit/save")
    @ApidocReturnR
    public Result save(@ApidocParam(name = "保存的供应商授信额度表,即集团给供应商的额度对象值", required = true) @RequestBody SupplierCreditAddDto supplierCredit) {
        try {
            supplierCreditService.saveSupplierCredit(supplierCredit, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * updateAndSaveSupplier
     * 修改
     */
    @PutMapping("/update")
    @ApidocReturnR
    public Result update(@ApidocParam(name = "修改的供应商授信额度表,即集团给供应商的额度对象值", required = true) @RequestBody SupplierCreditUpdateDto supplierCredit) {
        try {
            supplierCreditService.updateSupplierCreditById(supplierCredit, this.getUser());
            return Result.ok();
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApidocReturnR
    public Result delete(@ApidocParam(name = "删除的供应商授信额度表,即集团给供应商的额度对象id集合", required = true) @RequestBody Integer[] ids) {
        supplierCreditService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }


}
